<template>
  <a-spin :spinning="confirmLoading">
    <a-form ref="formRef" class="antd-modal-form" :labelCol="labelCol" :wrapperCol="wrapperCol">
      <a-row>
        <a-col :span="24">
          <a-form-item label="用户账号" v-bind="validateInfos.username">
            <a-input v-model:value="formData.username" placeholder="请输入用户账号" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="用户姓名" v-bind="validateInfos.realname">
            <a-input v-model:value="formData.realname" placeholder="请输入用户姓名" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="用户年龄" v-bind="validateInfos.age">
            <a-input v-model:value="formData.age" placeholder="请输入用户年龄" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="用户电话" v-bind="validateInfos.phone">
            <a-input v-model:value="formData.phone" placeholder="请输入用户电话" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="用户地址" v-bind="validateInfos.location">
            <a-input v-model:value="formData.location" placeholder="请输入用户地址" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="产品号码" v-bind="validateInfos.productNumber">
            <a-input v-model:value="formData.productNumber" placeholder="请输入产品号码" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="产品实例ID" v-bind="validateInfos.productExampleId">
            <a-input v-model:value="formData.productExampleId" placeholder="请输入产品实例ID" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="合约名称" v-bind="validateInfos.contractName">
            <a-input v-model:value="formData.contractName" placeholder="请输入合约名称" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="合约结束时间" v-bind="validateInfos.contractEndTime">
            <a-input v-model:value="formData.contractEndTime" placeholder="请输入合约结束时间" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="主套餐名称" v-bind="validateInfos.mainMealName">
	          <a-textarea v-model:value="formData.mainMealName" rows="4" placeholder="请输入主套餐名称" :disabled="disabled"/>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="状态匹配" v-bind="validateInfos.stateMatching">
            <a-input v-model:value="formData.stateMatching" placeholder="请输入状态匹配" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="网格名称（模糊匹配）" v-bind="validateInfos.griddingName">
            <a-input v-model:value="formData.griddingName" placeholder="请输入网格名称（模糊匹配）" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="营业区(模糊匹配)" v-bind="validateInfos.businessQuarter">
            <a-input v-model:value="formData.businessQuarter" placeholder="请输入营业区(模糊匹配)" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="是否质差" v-bind="validateInfos.sfzc">
	          <j-dict-select-tag v-model:value="formData.sfzc" dictCode="yn" placeholder="请选择是否质差" :disabled="disabled"/>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="宽带装机地址" v-bind="validateInfos.kdzjdz">
	          <a-textarea v-model:value="formData.kdzjdz" rows="4" placeholder="请输入宽带装机地址" :disabled="disabled"/>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="宽带下行速率" v-bind="validateInfos.kdxxsl">
	          <a-input-number v-model:value="formData.kdxxsl" placeholder="请输入宽带下行速率" style="width: 100%" :disabled="disabled"/>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="是否E8-C终端" v-bind="validateInfos.e8C">
	          <j-dict-select-tag v-model:value="formData.e8C" dictCode="yn" placeholder="请选择是否E8-C终端" :disabled="disabled"/>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="近三月账户下平均出账收入(分摊前)1" v-bind="validateInfos.jsgyzhpjczsr">
            <a-input v-model:value="formData.jsgyzhpjczsr" placeholder="请输入近三月账户下平均出账收入(分摊前)1" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="是否千兆光猫" v-bind="validateInfos.sfqzgm">
	          <j-dict-select-tag v-model:value="formData.sfqzgm" dictCode="yn" placeholder="请选择是否千兆光猫" :disabled="disabled"/>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="是否终端硬合约" v-bind="validateInfos.sfzdyhy">
	          <j-dict-select-tag v-model:value="formData.sfzdyhy" dictCode="yn" placeholder="请选择是否终端硬合约" :disabled="disabled"/>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="是否端网不匹配" v-bind="validateInfos.sfdwbpp">
	          <j-dict-select-tag v-model:value="formData.sfdwbpp" dictCode="yn" placeholder="请选择是否端网不匹配" :disabled="disabled"/>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="是否近三个月障碍" v-bind="validateInfos.sfjsgyzi">
	          <j-dict-select-tag v-model:value="formData.sfjsgyzi" dictCode="yn" placeholder="请选择是否近三个月障碍" :disabled="disabled"/>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="是否中高值" v-bind="validateInfos.sfzgz">
	          <j-dict-select-tag v-model:value="formData.sfzgz" dictCode="yn" placeholder="请选择是否中高值" :disabled="disabled"/>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="欠费金额(截止昨日)" v-bind="validateInfos.amountInArrear">
	          <a-input-number v-model:value="formData.amountInArrear" placeholder="请输入欠费金额(截止昨日)" style="width: 100%" :disabled="disabled"/>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="终端速率" v-bind="validateInfos.terminalRate">
	          <a-input-number v-model:value="formData.terminalRate" placeholder="请输入终端速率" style="width: 100%" :disabled="disabled"/>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="省" v-bind="validateInfos.province">
            <a-input v-model:value="formData.province" placeholder="请输入省" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="市" v-bind="validateInfos.city">
            <a-input v-model:value="formData.city" placeholder="请输入市" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="区域" v-bind="validateInfos.area">
            <a-input v-model:value="formData.area" placeholder="请输入区域" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="光猫型号" v-bind="validateInfos.model">
            <a-input v-model:value="formData.model" placeholder="请输入光猫型号" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="光猫是否不匹配" v-bind="validateInfos.gmsfbpp">
            <a-input v-model:value="formData.gmsfbpp" placeholder="请输入光猫是否不匹配" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="游戏网站名称" v-bind="validateInfos.gameName">
            <a-input v-model:value="formData.gameName" placeholder="请输入游戏网站名称" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="游戏下行累计流量(M)" v-bind="validateInfos.gameDown">
            <a-input v-model:value="formData.gameDown" placeholder="请输入游戏下行累计流量(M)" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="游戏上行累计流量(M)" v-bind="validateInfos.gameUp">
            <a-input v-model:value="formData.gameUp" placeholder="请输入游戏上行累计流量(M)" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="日均时长/游戏总时长(h)" v-bind="validateInfos.gameDaot">
            <a-input v-model:value="formData.gameDaot" placeholder="请输入日均时长/游戏总时长(h)" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="日均流量/游戏总流量(M)" v-bind="validateInfos.gameRateOfFlow">
            <a-input v-model:value="formData.gameRateOfFlow" placeholder="请输入日均流量/游戏总流量(M)" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="游戏质差次数" v-bind="validateInfos.gameZccs">
            <a-input v-model:value="formData.gameZccs" placeholder="请输入游戏质差次数" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="游戏质差时长占比(%)" v-bind="validateInfos.gameProportion">
            <a-input v-model:value="formData.gameProportion" placeholder="请输入游戏质差时长占比(%)" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="游戏上行平均时延(ms)" v-bind="validateInfos.gameUpDefer">
            <a-input v-model:value="formData.gameUpDefer" placeholder="请输入游戏上行平均时延(ms)" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="游戏下行平均时延(ms)" v-bind="validateInfos.gameDownDefer">
            <a-input v-model:value="formData.gameDownDefer" placeholder="请输入游戏下行平均时延(ms)" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="游戏上行平均抖动(ms)" v-bind="validateInfos.gameUpShake">
            <a-input v-model:value="formData.gameUpShake" placeholder="请输入游戏上行平均抖动(ms)" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="游戏下行平均抖动(ms)" v-bind="validateInfos.gameDownShake">
            <a-input v-model:value="formData.gameDownShake" placeholder="请输入游戏下行平均抖动(ms)" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="主播网站名称" v-bind="validateInfos.anchorName">
            <a-input v-model:value="formData.anchorName" placeholder="请输入主播网站名称" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="主播下行累计流量(M)" v-bind="validateInfos.anchorDown">
            <a-input v-model:value="formData.anchorDown" placeholder="请输入主播下行累计流量(M)" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="主播上行累计流量(M)" v-bind="validateInfos.anchorUp">
            <a-input v-model:value="formData.anchorUp" placeholder="请输入主播上行累计流量(M)" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="日均时长/主播总时长(h)" v-bind="validateInfos.anchorDaot">
            <a-input v-model:value="formData.anchorDaot" placeholder="请输入日均时长/主播总时长(h)" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="日均流量/主播总流量(M)" v-bind="validateInfos.anchorRateOfFlow">
            <a-input v-model:value="formData.anchorRateOfFlow" placeholder="请输入日均流量/主播总流量(M)" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="主播质差次数" v-bind="validateInfos.anchorZccs">
            <a-input v-model:value="formData.anchorZccs" placeholder="请输入主播质差次数" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="主播质差时长占比(%)" v-bind="validateInfos.anchorProportion">
            <a-input v-model:value="formData.anchorProportion" placeholder="请输入主播质差时长占比(%)" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="主播上行平均时延(ms)" v-bind="validateInfos.anchorUpDefer">
            <a-input v-model:value="formData.anchorUpDefer" placeholder="请输入主播上行平均时延(ms)" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="主播下行平均时延(ms)" v-bind="validateInfos.anchorDownDefer">
            <a-input v-model:value="formData.anchorDownDefer" placeholder="请输入主播下行平均时延(ms)" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="主播上行平均抖动(ms)" v-bind="validateInfos.anchorUpShake">
            <a-input v-model:value="formData.anchorUpShake" placeholder="请输入主播上行平均抖动(ms)" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="主播下行平均抖动(ms)" v-bind="validateInfos.anchorDownShake">
            <a-input v-model:value="formData.anchorDownShake" placeholder="请输入主播下行平均抖动(ms)" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
      </a-row>
    </a-form>
  </a-spin>
</template>

<script lang="ts" setup>
  import { ref, reactive, defineExpose, nextTick, defineProps, computed, onMounted } from 'vue';
  import { defHttp } from '/@/utils/http/axios';
  import { useMessage } from '/@/hooks/web/useMessage';
  import JDictSelectTag from '/@/components/Form/src/jeecg/components/JDictSelectTag.vue';
  import { getValueType } from '/@/utils';
  import { saveOrUpdate } from '../SyMarketingDepartment.api';
  import { Form } from 'ant-design-vue';
  
  const props = defineProps({
    formDisabled: { type: Boolean, default: false },
    formData: { type: Object, default: ()=>{} },
    formBpm: { type: Boolean, default: true }
  });
  const formRef = ref();
  const useForm = Form.useForm;
  const emit = defineEmits(['register', 'ok']);
  const formData = reactive<Record<string, any>>({
    id: '',
    username: '',   
    realname: '',   
    age: '',   
    phone: '',   
    location: '',   
    productNumber: '',   
    productExampleId: '',   
    contractName: '',   
    contractEndTime: '',   
    mainMealName: '',   
    stateMatching: '',   
    griddingName: '',   
    businessQuarter: '',   
    sfzc: '',   
    kdzjdz: '',   
    kdxxsl: undefined,
    e8C: '',   
    jsgyzhpjczsr: '',   
    sfqzgm: '',   
    sfzdyhy: '',   
    sfdwbpp: '',   
    sfjsgyzi: '',   
    sfzgz: '',   
    amountInArrear: undefined,
    terminalRate: undefined,
    province: '',   
    city: '',   
    area: '',   
    model: '',   
    gmsfbpp: '',   
    gameName: '',   
    gameDown: '',   
    gameUp: '',   
    gameDaot: '',   
    gameRateOfFlow: '',   
    gameZccs: '',   
    gameProportion: '',   
    gameUpDefer: '',   
    gameDownDefer: '',   
    gameUpShake: '',   
    gameDownShake: '',   
    anchorName: '',   
    anchorDown: '',   
    anchorUp: '',   
    anchorDaot: '',   
    anchorRateOfFlow: '',   
    anchorZccs: '',   
    anchorProportion: '',   
    anchorUpDefer: '',   
    anchorDownDefer: '',   
    anchorUpShake: '',   
    anchorDownShake: '',   
  });
  const { createMessage } = useMessage();
  const labelCol = ref<any>({ xs: { span: 24 }, sm: { span: 5 } });
  const wrapperCol = ref<any>({ xs: { span: 24 }, sm: { span: 16 } });
  const confirmLoading = ref<boolean>(false);
  //表单验证
  const validatorRules = {
  };
  const { resetFields, validate, validateInfos } = useForm(formData, validatorRules, { immediate: true });

  // 表单禁用
  const disabled = computed(()=>{
    if(props.formBpm === true){
      if(props.formData.disabled === false){
        return false;
      }else{
        return true;
      }
    }
    return props.formDisabled;
  });

  
  /**
   * 新增
   */
  function add() {
    edit({});
  }

  /**
   * 编辑
   */
  function edit(record) {
    nextTick(() => {
      resetFields();
      //赋值
      Object.assign(formData, record);
    });
  }

  /**
   * 提交数据
   */
  async function submitForm() {
    // 触发表单验证
    await validate();
    confirmLoading.value = true;
    const isUpdate = ref<boolean>(false);
    //时间格式化
    let model = formData;
    if (model.id) {
      isUpdate.value = true;
    }
    //循环数据
    for (let data in model) {
      //如果该数据是数组并且是字符串类型
      if (model[data] instanceof Array) {
        let valueType = getValueType(formRef.value.getProps, data);
        //如果是字符串类型的需要变成以逗号分割的字符串
        if (valueType === 'string') {
          model[data] = model[data].join(',');
        }
      }
    }
    await saveOrUpdate(model, isUpdate.value)
      .then((res) => {
        if (res.success) {
          createMessage.success(res.message);
          emit('ok');
        } else {
          createMessage.warning(res.message);
        }
      })
      .finally(() => {
        confirmLoading.value = false;
      });
  }


  defineExpose({
    add,
    edit,
    submitForm,
  });
</script>

<style lang="less" scoped>
  .antd-modal-form {
    min-height: 500px !important;
    overflow-y: auto;
    padding: 24px 24px 24px 24px;
  }
</style>
